<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="" prop="orderSn">
        <el-input
          v-model="queryParams.orderSn"
          placeholder="请输入订单号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入手机号码"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="" prop="isFollowUp">
        <el-select
          v-model="queryParams.isFollowUp"
          placeholder="是否跟进"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in isFollowUpList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="" prop="channel" v-if="searchShow">
        <el-select
          v-model="queryParams.channel"
          placeholder="渠道"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in channelList"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="" prop="twoExamineBy">
        <el-select
          v-model="queryParams.twoExamineBy"
          placeholder="审核人员"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in userList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="" prop="examineStatus">
        <el-select
          v-model="queryParams.examineStatus"
          placeholder="审核人员"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in examineStatusList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="" prop="firstApply">
        <el-select
          v-model="queryParams.firstApply"
          placeholder="贷款状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in applyStatusList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

<!--      <el-form-item label="" prop="applyLog">-->
<!--        <el-select-->
<!--          v-model="queryParams.applyLog"-->
<!--          placeholder="是否有还款记录"-->
<!--          clearable-->
<!--          style="width: 240px"-->
<!--        >-->
<!--          <el-option-->
<!--            v-for="item in applyLogList"-->
<!--            :key="item.value"-->
<!--            :label="item.label"-->
<!--            :value="item.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->

      <el-form-item label="">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="分配开始日期"
          end-placeholder="分配结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="handleUpdate"
        >批量分配
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column type="index" label="序号" width="50"/>
      <el-table-column label="平台" align="center" prop="companyId"/>
      <el-table-column label="渠道" align="center" prop="channelName"/>
      <el-table-column label="订单号" align="center" prop="orderSn"/>
      <el-table-column label="贷款用户" align="center" prop="userName"/>
      <el-table-column label="手机号码" align="center" prop="userPhone"/>
      <el-table-column label="贷款状态" align="center" prop="firstApply">
        <template slot-scope="scope">
              <span v-if="scope.row.firstApply == 0">
                首借
              </span>
          <span v-else>复借</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人员" align="center" prop="twoExamineBy">
        <template slot-scope="scope">
          <span v-for="item in userList" v-if="scope.row.twoExamineBy == item.userId">{{item.userName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.twoExamineBy">已分配</span>
          <span v-else>未分配</span>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="终审分配时间" align="center" prop="twoExamineTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.twoExamineTime) }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column label="打款时间" align="center" prop="applyTime" width="160">-->
        <!--<template slot-scope="scope">-->
          <!--<span>{{ parseTime(scope.row.applyTime) }}</span>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <!--<el-table-column label="还款时间" align="center" prop="repaymentTime" width="160">-->
        <!--<template slot-scope="scope">-->
          <!--<span>{{ parseTime(scope.row.repaymentTime) }}</span>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <el-table-column
        label="操作"
        align="center"
        width="160"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >订单分配
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 分配 -->
    <el-dialog title="订单分配" :visible.sync="open" width="400px">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-form-item label="审核人员" prop="twoExamineBy">
          <el-radio-group v-model="form.twoExamineBy">
            <el-radio
              v-for="dict in userList"
              :key="dict.userId"
              :label="dict.userId"
            >{{ dict.userName }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitTwoExamineByForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {listApply, getApply, examineApply, closeApply, listExamineUser,updateApplyTwoExamineBy,listTwoExamineApply} from "@/api/app/apply";
  import {channelList} from "@/api/app/channel";

  export default {
    name: "TwoExamine",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        id: null,
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        searchShow: this.$store.state.user.companyId ? false : true,
        // 总条数
        total: 0,
        // 订单表格数据
        applyList: null,
        // 审核人员数据
        userList: null,
        channelList: null,
        companyId: this.$store.state.user.companyId,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 日期范围
        dateRange: [],
        // 表单参数
        form: {},
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          twoExamineBy: undefined,
          userPhone: undefined,
          orderSn: undefined,
          firstApply: undefined,
          applyLog: undefined,
          isFollowUp: undefined,
          status: '2',
        },
        isFollowUpList: [
          {
            value: '0',
            label: '否'
          },
          {
            value: '2',
            label: '是'
          },
        ],
        applyLogList: [
          {
            value: '0',
            label: '否'
          },
          {
            value: '1',
            label: '是'
          },
        ],
        examineStatusList: [
          {
            id: '1',
            name: '未分配'
          },
          {
            id: '2',
            name: '已分配'
          }
        ],
        applyStatusList: [
          {
            id: '0',
            name: '首借'
          },
          {
            id: '2',
            name: '复借'
          }
        ],
        // 表单校验
        rules: {
          applyMoney: [
            {required: true, message: "贷款不能为空", trigger: "blur"},
          ]
        }
      };
    },
    watch: {},
    created() {
      if (this.$route.query && this.$route.query.pageNum) {
        this.queryParams.pageNum = parseInt(this.$route.query.pageNum);
        this.queryParams.pageSize = parseInt(this.$route.query.pageSize);
        this.queryParams.userPhone = this.$route.query.userPhone;
        this.queryParams.orderSn = this.$route.query.orderSn;
        this.queryParams.firstApply = this.$route.query.firstApply;
        this.queryParams.towExamineBy = this.$route.query.towExamineBy;
      }
      this.getList();
      this.getListExamineUser();
      this.getChannelList();
    },
    methods: {
      /** 查询列表 */
      getList() {
        this.loading = true;
        if (this.companyId){
          this.queryParams.companyId = this.companyId
        }
        listTwoExamineApply(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.applyList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },

      getChannelList(){
          channelList().then(response => {
            this.channelList = response.data;
          })
      },

      getListExamineUser() {
        listExamineUser().then(response => {
            this.userList = response.data;
          }
        );
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          twoExamineBy: undefined,
        };
        this.resetForm("form");
      },

      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },

      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },

      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },

      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.id = row.id;
        this.open = true;
      },

      submitTwoExamineByForm() {
        const applyIds = this.id || this.ids.join(",");
        this.$refs["form"].validate(valid => {
          updateApplyTwoExamineBy(this.form.twoExamineBy, applyIds).then(response => {
            this.$modal.msgSuccess("分配成功");
            this.open = false;
            this.id = null,
            this.getList();
          });
        });
      },



      /** 提交按钮 */
      submitForm: function () {
        // this.$refs["form"].validate(valid => {
        //   if (valid) {
        //     if (this.form.id != undefined) {
        //       updateIndexApply(this.form).then(response => {
        //         this.$modal.msgSuccess("修改成功");
        //         this.open = false;
        //         this.getList();
        //       });
        //     } else {
        //       addIndexApply(this.form).then(response => {
        //         this.$modal.msgSuccess("新增成功");
        //         this.open = false;
        //         this.getList();
        //       });
        //     }
        //   }
        // });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        // const ids = row.id || this.ids;
        // this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function () {
        //   return delIndexApply(ids);
        // }).then(() => {
        //   this.getList();
        //   this.$modal.msgSuccess("删除成功");
        // }).catch(() => {
        // });
      },

      // handleMoney(){
      //   this.form.realMoney = this.form.applyMoney - this.form.manageMoney - this.form.examineMoney - this.form.serviceMoney;
      // }
    }
  };
</script>
